package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    /**
     * 新增商机跟进记录
     */
    @Transactional
    @Override
    public AjaxResult add(BusinessTrackVo businessTrackVo) {
        //获取商机id
        Long businessId = businessTrackVo.getBusinessId();
        TbBusiness tbBusiness = new TbBusiness();
        //将前端传来的数据复制到新的商机对象中
        BeanUtils.copyProperties(businessTrackVo, tbBusiness);
        tbBusiness.setId(businessId);
        tbBusiness.setStatus(TbBusiness.StatusType.FOLLOWING.getValue());
        //把数据库中对应的数据修改成新的数据
        tbBusinessMapper.updateTbBusiness(tbBusiness);
        //将前端传来的数据复制到新的商机记录对象中
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo, tbBusinessTrackRecord);
        tbBusinessTrackRecord.setCreateTime(DateUtils.getNowDate());
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);
        return AjaxResult.success("新增成功");
    }

    /**
     * 查询商机跟进记录列表
     */
    @Override
    public List<TbBusinessTrackRecord> list(Long id) {
        List<TbBusinessTrackRecord> list = tbBusinessTrackRecordMapper.list(id);
        return list;
    }
}
